package com.idempotent.demo.controller;

import com.idempotent.demo.dto.OrderDTO;
import com.idempotent.demo.entity.Order;
import com.idempotent.demo.service.IdempotentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/idempotent")
public class IdempotentController {

    @Autowired
    private IdempotentService idempotentService;


    // ------------------------------ 1. Redis Token方式 ------------------------------
    /**
     * 获取Redis Token
     */
    @GetMapping("/redis/token")
    public ResponseEntity<String> getRedisToken() {
        return ResponseEntity.ok(idempotentService.generateRedisToken());
    }

    /**
     * Redis Token方式创建订单
     */
    @PostMapping("/redis/create")
    public ResponseEntity<Order> createWithRedis(
            @RequestHeader("Redis-Token") String token,
            @RequestBody OrderDTO dto) {
        Order order = idempotentService.createOrderWithRedisToken(token, dto);
        return ResponseEntity.ok(order);
    }


    // ------------------------------ 2. Redisson方式 ------------------------------
    /**
     * 获取Redisson Token
     */
    @GetMapping("/redisson/token")
    public ResponseEntity<String> getRedissonToken() {
        return ResponseEntity.ok(idempotentService.generateRedissonToken());
    }

    /**
     * Redisson方式创建订单
     */
    @PostMapping("/redisson/create")
    public ResponseEntity<Order> createWithRedisson(
            @RequestHeader("Redisson-Token") String token,
            @RequestBody OrderDTO dto) {
        Order order = idempotentService.createOrderWithRedisson(token, dto);
        return ResponseEntity.ok(order);
    }


    // ------------------------------ 3. 数据库唯一约束方式 ------------------------------
    /**
     * 数据库唯一约束方式创建订单
     */
    @PostMapping("/db/unique/create")
    public ResponseEntity<Order> createWithDbUnique(@RequestBody OrderDTO dto) {
        Order order = idempotentService.createOrderWithDbUnique(dto);
        return ResponseEntity.ok(order);
    }


    // ------------------------------ 4. 数据库乐观锁方式 ------------------------------
    /**
     * 乐观锁方式更新订单状态
     */
    @PutMapping("/db/version/update")
    public ResponseEntity<Order> updateWithVersion(
            @RequestParam Long orderId,
            @RequestParam Integer newStatus,
            @RequestParam Integer currentVersion) {
        Order order = idempotentService.updateOrderWithVersion(orderId, newStatus, currentVersion);
        return ResponseEntity.ok(order);
    }


    // ------------------------------ 5. 请求唯一ID（requestId）方式 ------------------------------
    /**
     * 基于requestId创建订单
     */
    @PostMapping("/request-id/create")
    public ResponseEntity<Order> createWithRequestId(
            @RequestHeader("Request-Id") String requestId,
            @RequestBody OrderDTO dto) {
        Order order = idempotentService.createOrderWithRequestId(requestId, dto);
        return ResponseEntity.ok(order);
    }


    // 健康检查接口
    @GetMapping("/health")
    public ResponseEntity<Map<String, String>> health() {
        Map<String, String> result = new HashMap<>();
        result.put("status", "UP");
        result.put("message", "幂等性服务运行正常");
        return ResponseEntity.ok(result);
    }
}
    